﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Menu.cs
* 创建标识：曾乃强 15158756652 2011-03-14 03:03:16
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using CNVP.Config;

namespace CNVP.JShop.BLL
{
    public class BLLMenu : CNVP_Menu
    {
        private string Prefix = DBConfig.Prefix;
        public void BuildAllTree(System.Web.UI.WebControls.TreeView tree)
        {
            tree.Nodes.Clear();
            var allitems = base.GetAll_CNVP_Menu();
            var topitems = base.GetAll_CNVP_Menu("Depth=0 order by Weight");
            foreach (var item in topitems)
            {
                TreeNode node = new TreeNode()
                {
                    Value = item.ID.ToString(),
                    Text = item.MenuName
                };

                BuildPartTree(node, item.ID, allitems);

                tree.Nodes.Add(node);
            }
            tree.ExpandAll();
        }

        public void BuildPartTree(TreeNode currentNode, int currentMenuID, List<Model.CNVP_Menu> datasource)
        {
            var subMenus = base.GetAll_CNVP_Menu("Depth=" + currentMenuID + " order by weight");
            foreach (var item in subMenus)
            {
                TreeNode node = new TreeNode()
                {
                    Value = item.ID.ToString(),
                    Text = item.MenuName
                };
                this.BuildPartTree(node, item.ID, datasource);

                currentNode.ChildNodes.Add(node);
            }

        }

        /// <summary>
        /// 栏目,权限
        /// </summary>
        /// <param name="label"></param>
        /// <param name="Depth"></param>
        public void BuildAlllabel(System.Web.UI.WebControls.Literal label, int Depth, string userName, int AdminRoleID)
        {
            //取出用户角色ID
            var allitems = base.GetAll_CNVP_Menu();
            var topitems = base.GetAll_CNVP_Menu("Depth=" + Depth + " AND ID in (select MenuID from " + Prefix + "AdminRoleMenu where AdminRoleID=" + AdminRoleID + ") order by Weight");
            foreach (var item in topitems)
            {
                var subMenus = base.GetAll_CNVP_Menu("Depth=" + item.ID + " and ID in (select MenuID from " + Prefix + "AdminRoleMenu where AdminRoleID=" + AdminRoleID + ") order by weight");
                if (subMenus.Count > 0)
                {
                    string html = "";
                    html += "<dl class=\"MenuItem\">";
                    html += "<dt class=\"MenuItemTitle\"><span class=\"TitleWord\">" + item.MenuName + "</span></dt>";
                    html += "</dl>";
                    label.Text += html;
                    this.BuildPartlabel(label, item.ID, allitems, AdminRoleID);
                }
            }
            
        }
        public void BuildPartlabel(System.Web.UI.WebControls.Literal label, int currentMenuID, List<Model.CNVP_Menu> datasource, int AdminRoleID)
        {
            var subMenus = base.GetAll_CNVP_Menu("Depth=" + currentMenuID + " and ID in (select MenuID from " + Prefix + "AdminRoleMenu where AdminRoleID=" + AdminRoleID + ") order by weight");
            foreach (var item in subMenus)
            {
                string html = "";
                html += "<dl class=\"MenuItem2\">";
                html += "<li class=\"SubMenuItemListItem\">";
                html += "		<a  href=\"" + item.MenuUrl + "\" target =\"Content\" >" + item.MenuName + "</a>";
                html += "	</li>";
                html += "</dl>";
                label.Text += html;
                this.BuildPartlabel(label, item.ID, datasource, AdminRoleID);
            }
            
        }

        /// <summary>
        /// 权限,设置
        /// </summary>
        /// <param name="label"></param>
        public void BuildAllPowerlabel(System.Web.UI.WebControls.Label label, int AdminRoleID)
        {
            var allitems = base.GetAll_CNVP_Menu();
            var topitems = base.GetAll_CNVP_Menu("Depth=0 order by Weight");
            foreach (var item in topitems)
            {
                string vv = "&nbsp;&nbsp;";
                string html = "";
                html += "<div class='menuGroup'>" + item.MenuName + "</div>";
                label.Text += html;
                this.BuildPartPowerlabel(label, item.ID, allitems, AdminRoleID, vv);
            }
        }
        public void BuildPartPowerlabel(System.Web.UI.WebControls.Label label, int currentMenuID, List<Model.CNVP_Menu> datasource, int AdminRoleID, string vv)
        {
            vv += vv;
            var subMenus = base.GetAllPower_CNVP_Menu(currentMenuID, AdminRoleID);
            foreach (var item in subMenus)
            {
                string html = "";
                html += "<div id='menuItem' name='menuItem'>" + vv + "<input type='checkbox' " + item.Flag + " name=\"cbMenu\" id=\"cbMenu" + item.ID + "\" value=\"" + item.ID + "\" /><label>" + item.MenuName + "</label></div>";
                label.Text += html;
                this.BuildPartPowerlabel(label, item.ID, datasource, AdminRoleID, vv);
            }
        }

        public string GetClass(int ParentID, string Url)
        {
            DataTable Dt = base.GetAll_CNVP_Menu2("Depth=" + ParentID + " order by weight");
            return DtToJSON(Url, Dt);
        }
        private string DtToJSON(string Url, DataTable Dt)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("{");
            Str.Append("nodes:[");
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                Str.Append("{");
                //判断是否存在下级节点
                if (IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                {
                    Str.Append("source:\"MenuAjax.aspx?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\",");
                }
                Str.Append("text:\"" + Dt.Rows[i]["MenuName"].ToString() + "\"");
                Str.Append(",url:\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                Str.Append("},");
            }
            Str.Append("]");
            Str.Append("}");
            string xx = Str.ToString();
            return Str.ToString();
        }
        private bool IsChildMenu(int ParentID)
        {
            bool flg = false;
            var IsChildMenu_List = new BLL.CNVP_Menu().GetAll_CNVP_Menu("Depth=" + ParentID + " order by weight");
            if (IsChildMenu_List.Count > 0)
            {
                flg = true;
            }
            return flg;
        }

    }
}
